package com.sync.utils.project.demo.controller;

import com.sync.utils.common.base.controller.impl.BaseControllerImpl;
import com.sync.utils.framework.result.R;
import com.sync.utils.project.demo.MineExecutorsFactory;
import com.sync.utils.project.demo.MyThread;
import com.sync.utils.project.demo.pojo.UserRes;
import com.sync.utils.project.demo.service.PollingOrderService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2023/6/9 11:07
 */
@Slf4j
@RestController
public class MineExecutorsController extends BaseControllerImpl {

    @Autowired
    private PollingOrderService pollingOrderService;

    public final ScheduledExecutorService scheduledExecutorService = MineExecutorsFactory.scheduler;
    private final static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");

    @GetMapping(value = "syncDemo")
    public R<UserRes> syncDemo() {
        ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(new MyThread(1), 0, 3, TimeUnit.SECONDS);
        Object s = null;
        try {
            s = scheduledFuture.get();
            System.out.println("执行到这儿:" + s + " " + Thread.currentThread().getName());
        } catch (InterruptedException e) {
            log.error("Interrupted while");
        } catch (ExecutionException e) {
            log.error("执行结束");
        }
        System.out.println("执行完了:" + s + " " + Thread.currentThread().getName());
        return R.ok().data("执行成功了");
    }

    @GetMapping("syncDemo02")
    public R syncDemo02() throws ExecutionException, InterruptedException {
        ScheduledFuture<?> sf = scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            int count = 1;

            @SneakyThrows
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName() + "执行了第" + count + "次");
                Thread.sleep(3000);
                if (count == 5) {
                    System.out.println(Thread.currentThread().getName() + "执行了第" + count + "次,执行结束，拿到结果");
                    scheduledExecutorService.isShutdown();
                }
                count++;
            }
        }, 1, 2, TimeUnit.SECONDS);
        sf.get();
        return R.ok().data("");
    }


    @GetMapping(value = "syncDemo03")
    public R syncDemo03(@RequestParam("orderNo") String orderNo) {
        Map<String, String> map = new HashMap();
        map.put("orderNo", orderNo);
        Map<String, String> result = pollingOrderService.pollingOrder(map);
        return R.ok().data(result);
    }


}
